package handlers

import (
	"encoding/json"
	"log/slog"
	"lsp-server/internal/protocol"
)

const (
	TextDocumentSyncKindNone protocol.TextDocumentSyncKind = iota
	TextDocumentSyncKindFull
	TextDocumentSyncKindIncremental
)

const InitializeMethod = "initialize"

type Initialize struct {
}

func (i Initialize) Call(params json.RawMessage) (any, error) {
	slog.Info("received initialize method")
	iParams, err := i.parseParams(params)
	if err != nil {
		slog.Error("Error to parse initialized params")
		return nil, err
	}
	slog.Debug("initialized params", slog.Any("params", *iParams))

	result := protocol.InitializeResult{
		Capabilities: protocol.ServerCapabilities{
			TextDocumentSync:   TextDocumentSyncKindFull,
			HoverProvider:      &protocol.Or_ServerCapabilities_hoverProvider{},
			CompletionProvider: &protocol.CompletionOptions{},
		},
	}

	return result, nil
}

func (i Initialize) parseParams(params json.RawMessage) (*protocol.InitializedParams, error) {
	var initializeParams protocol.InitializedParams
	if err := json.Unmarshal(params, &initializeParams); err != nil {
		return nil, err
	}

	return &initializeParams, nil
}
